package com.mustafaiev.tair.cts.tapestry.pages.batches;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import com.mustafaiev.tair.cts.facade.IBasketsBatchFacade;
import com.mustafaiev.tair.cts.factory.ICostTrackingSystemFacadeFactory;
import com.mustafaiev.tair.cts.tapestry.dto.BasketsBatchDTO;

public class Index {

	@Inject
	private ICostTrackingSystemFacadeFactory facadeFactory;

	@InjectPage
	private CreateBasketsBatch createBasketsBatch;

	@Property
	@Persist
	private BasketsBatchDTO batch;

	@Property
	@Persist
	private List<BasketsBatchDTO> batches;

	@Property
	private BeanModel<BasketsBatchDTO> batchesGridModel;

	@Inject
	private BeanModelSource beanModelSource;

	@Inject
	private Messages messages;

	@SetupRender
	public void setupRender() {
		this.batch = new BasketsBatchDTO();
		this.batches = getFacade().retreiveBatches();

		this.batchesGridModel = this.beanModelSource.createDisplayModel(
				BasketsBatchDTO.class, this.messages);
	}

	@OnEvent(value = "create")
	public Object onCreate() {
		return this.createBasketsBatch;
	}

	private IBasketsBatchFacade getFacade() {
		return this.facadeFactory.getFacade(IBasketsBatchFacade.class);
	}
}
